package com.tiger.novelAvinfo;

import java.util.List;

import com.tiger.novelAvinfo.base.BaseActivity;
import com.tiger.novelAvinfo.base.CommonUtil;
import com.tiger.novelAvinfo.base.DataServer;
import com.tiger.novelAvinfo.base.DataServerFactory;
import com.tiger.novelAvinfo.base.DataServer.IServerResponseListener;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MenuActivity extends BaseActivity {
	private DataServer server = DataServer.getInstance(this);
	private DataServerFactory serverData = DataServerFactory.getInstance();
	private ServerResponseListener listener=new ServerResponseListener();
	private ProgressDialog pd_downloading;
	private ListView contactList;
	private List<AVInfo> infos;
	private ContactAdapter adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.menu);
        server.addListener(CommonUtil.listnerKeys.read_key, listener);
        CommonUtil.setAdView(this, this);
        initView();
        pd_downloading = ProgressDialog.show(MenuActivity.this, null,"正在加载目录，请稍候",
				true, true);
        server.sendReqGetMenu(CommonUtil.sendMessage.READ_TXT);
    }
    private void initView() {
    	adapter = new ContactAdapter(this);
		contactList = (ListView) this.findViewById(R.id.menu_list);
		contactList.setAdapter(adapter);
		contactList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View item,
					int position, long itemId) {
				ContactInfo info = (ContactInfo) item.getTag();
				if (info != null) {
					Bundle bundle=new Bundle();
					String enName=info.entity.getEnglish_Name();
					String zhName=info.entity.getChinese_Name();
					bundle.putString("enName", enName);
					bundle.putString("zhName", zhName);
					bundle.putInt("avNum", position);//序号
					Intent intent=new Intent(MenuActivity.this,LookActivity.class);
					intent.putExtras(bundle);
					MenuActivity.this.startActivity(intent);
				}
			}
		});
    }
    @Override
	protected void onDestroy() {
    	server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	@Override
	protected void onPause() {
		server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onPause();
	}
	@Override
	protected void onResume() {
		server.addListener(CommonUtil.listnerKeys.read_key, listener);
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	protected void onStop() {
		server.removeListener(CommonUtil.listnerKeys.read_key);
		// TODO Auto-generated method stub
		super.onStop();
	}
    class ContactInfo {
		public TextView username;
		public AVInfo entity;
	}

	public class ContactAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public ContactAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (infos == null)
				return 0;
			return infos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ContactInfo holder = null;
			if (convertView == null) {
				holder = new ContactInfo();
				convertView = mInflater.inflate(R.layout.menu_item, null);
				holder.username = (TextView) convertView
						.findViewById(R.id.username);
			} else {
				holder = (ContactInfo) convertView.getTag();
			}

			if (infos != null) {
				holder.username.setText(infos.get(position).getChinese_Name());
				holder.entity=infos.get(position);
			}
			convertView.setTag(holder);
			return convertView;
		}

	}
	 Handler handler=new Handler(){
	    	@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case CommonUtil.sendMessage.READ_TXT:
					//after get data,do something
					adapter.notifyDataSetChanged();
					if(pd_downloading!=null)
					{
						pd_downloading.dismiss();
						pd_downloading=null;
					}
					break;
				}
	    	}
	    };
	public class ServerResponseListener implements IServerResponseListener{
		@Override
		public void handleResponse(int reqId, boolean result) {
			// TODO Auto-generated method stub
			if (result) {
				switch (reqId) {
				case CommonUtil.sendMessage.READ_TXT:
					//get data
					infos=serverData.getInfos();
					handler.sendEmptyMessage(CommonUtil.sendMessage.READ_TXT);
					break;
				default:
					break;
				}
			} else {
				if(pd_downloading!=null)
				{
					pd_downloading.dismiss();
					pd_downloading=null;
				}
				Toast.makeText(MenuActivity.this, "读取资源失败，请重试",
						Toast.LENGTH_SHORT).show();
			}
		}
		
	}
}